package com.shadow.shadow_lib.weibo.sina;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

import android.content.Context;
import android.os.Bundle;

import com.shadow.shadow_lib.weibo.base.WeiboTokenBean;
import com.shadow.shadow_lib.weibo.base.WeiboUtil;

public class SinaTokenBean extends WeiboTokenBean {
	private static String SHARE_PREFERENCES_SINA = "share_preferences_sina";

	public SinaTokenBean(Context context) {
		super(context, SHARE_PREFERENCES_SINA, SinaAuthorizeActivity.class);
	}

	public String getAccessWithCode(String str) {
		String pairs[] = str.split("&");
		for (int i = 0; i < pairs.length; i++) {
			if (pairs[i].startsWith("code")) {
				String code_key_value = pairs[i];
				String code = code_key_value.split("[=]")[1];
				Bundle b = new Bundle();
				b.putString("code", code);
				b.putString("client_id", SinaWeiboConstant.APP_KEY);
				b.putString("client_secret", SinaWeiboConstant.APP_SECRET);
				b.putString("redirect_uri",
						SinaWeiboConstant.CANCEL_REDIRECT_URI);
				b.putString("grant_type", "authorization_code");
				HttpURLConnection connection = WeiboUtil
						.getAURLConnection("http://api.weibo.com/oauth2/access_token");
				WeiboUtil.postConnection(connection, b, null);
				try {
					BufferedReader br = new BufferedReader(new InputStreamReader(
							connection.getInputStream()));
					System.out.println(br.readLine());
					br.close();
					connection.disconnect();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return null;
	}
}
